﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Text;

namespace T.FAS.Runtime.Base.Entity
{
    public static class ViewModelResultBuilder
    {
        /// <summary>
        /// 构造viewmodel数据集合
        /// </summary>
        /// <param name="ds">数据集合</param>
        /// <param name="schema">viewmodel结构</param>
        /// <returns></returns>
        public static ViewModelResult BuildViewModelResult(DataSet ds, ViewModelSchema schema,DataType dType)
        {
            if (ds == null || ds.Tables == null || ds.Tables.Count <= 0)
                throw new ArgumentNullException("ViewModelResultBuilder.BuildViewModelResult Error,DS is null");
            if (schema == null || schema.Current == null || schema.Current.Count <= 0)
                throw new ArgumentNullException("ViewModelResultBuilder.BuildViewModelResult Error,shcema is null");
            var result = new ViewModelResult();
            result.DataResultType = dType;
            result.Schema = schema;
            result.Count = 0;
            result.Record = new TRecord();
            result.List = new ListTRecord();
            if (dType == DataType.Record)
            {
                if (ds.Tables[schema.MainViewModelItemCode].Rows.Count > 0)
                {
                    var propertiesDic = new Dictionary<string, object>();
                    schema.Current.ForEach(item =>
                    {
                        TRecord data = BuildRecord(ds.Tables[schema.MainViewModelItemCode].Rows[0],ds,schema.MainViewModelItem.ViewModelItemSchema);
                        propertiesDic.Add(item.ViewModelItemCode, data);
                    });
                    result.Record = new TRecord(propertiesDic);
                    result.Count = 1;
                    result.List.Add(result.Record);
                    result.List.Rows = 1;
                }
            }
            else
            {
                foreach (DataRow row in ds.Tables[schema.MainViewModelItemCode].Rows)
                {
                    var propertiesDic = new Dictionary<string, object>();
                    schema.Current.ForEach(item =>
                    {
                        TRecord data = BuildRecord(row, ds, schema.MainViewModelItem.ViewModelItemSchema);
                        propertiesDic.Add(item.ViewModelItemCode, data);
                    });
                    TRecord dataResult = new TRecord(propertiesDic);
                    result.List.Add(dataResult);
                }
                result.List.Rows = ds.Tables[schema.MainViewModelItemCode].Rows.Count;
            }
            return result;
        }
        /// <summary>
        /// 构造viewmodelitem数据集合
        /// </summary>
        /// <param name="ds">数据集合</param>
        /// <param name="vmis">viewmodelitem结构</param>
        /// <param name="parentData">关联数据</param>
        /// <param name="isList">是否列表</param>
        /// <param name="isRelated">是否是数据关联</param>
        /// <returns></returns>
        private static TRecord BuildRecord(DataRow row,DataSet ds, ViewModelItemSchema vmis, string relatedData = "", bool isList = false, bool isRelated = false)
        {
            TRecord record = new TRecord();
            var propertiesDic = new Dictionary<string, object>();
            vmis.Properties.ForEach(p =>
            {
                if (!p.IsVirtual)
                {
                    if (p.DataType == DataType.List)
                    {
                        var listDataTable = ds.Tables[p.SubSchema.ViewModelItemCode];
                        propertiesDic.Add(p.Code, BuildRecords(listDataTable, ds, p.SubSchema));
                    }
                    else if (p.DataType == DataType.Record)
                    {
                        var subTable = ds.Tables[p.SubSchema.ViewModelItemCode];
                        var subRow = subTable.Select($"{p.SubSchema.Relation.ToPropertyCode}='{Convert.ToString(row[p.SubSchema.Relation.FromPropertyCode])}'")[0];
                        propertiesDic.Add(p.Code,BuildRecord(subRow,ds,p.SubSchema));
                    }
                    else
                    {
                        propertiesDic.Add(p.Code,row[p.Code]);
                    }
                }
            });
            record = new TRecord(propertiesDic);
            return record;
        }

        private static List<TRecord> BuildRecords(DataTable dt,DataSet ds, ViewModelItemSchema vmis)
        {
            List<TRecord> list = new List<TRecord>();
            foreach (DataRow row in dt.Rows)
            {
                TRecord data = BuildRecord(row, ds, vmis);
                list.Add(data);
            }
            return list;
        }


    }
}
